package logic

import (
	"context"
	"errors"
	"newwechat/models/mysql"

	"newwechat/wechatrpc/internal/svc"
	"newwechat/wechatrpc/wechatrpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type PerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectLogic {
	return &PerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 完善用户信息
func (l *PerfectLogic) Perfect(in *wechatrpc.PerfectRequest) (*wechatrpc.PerfectResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.FindUserByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户不存在")
	}
	users := &mysql.User{
		Model:     gorm.Model{ID: user.ID},
		HeadImage: in.HeadImage,
		NickName:  in.NickName,
		Address:   in.Address,
		Age:       in.Age,
		Qq:        in.Qq,
		Email:     in.Email,
	}
	err = users.UserUpdate(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &wechatrpc.PerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
